package com.piece.mall.service;

import com.piece.core.framework.util.collection.MapUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.mall.model.PmsSkuInfo;
import com.piece.mall.model.SmsMemberPrice;
import com.piece.mall.model.SmsSkuFullReduction;
import com.piece.mall.model.SmsSkuLadder;
import com.piece.mall.repository.SmsSkuFullReductionRepository;
import com.piece.mall.vo.MemberPriceVo;
import com.piece.mall.vo.SkuInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SmsSkuFullReductionService extends BaseService<SmsSkuFullReduction, String> {

    @Autowired
    private SmsSkuFullReductionRepository smsSkuFullReductionRepository;

    @Lazy
    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @Resource
    private SmsSkuLadderService smsSkuLadderService;

    @Resource
    private SmsMemberPriceService smsMemberPriceService;

    @PostConstruct
    public void initRepository() {
        setRepository(smsSkuFullReductionRepository);
    }

    public SkuInfoVo findSkuReduction(Long skuId) {
        PmsSkuInfo skuInfo = pmsSkuInfoService.findById(skuId);
        SkuInfoVo skuVo = new SkuInfoVo();
        // 1.基本信息
        skuVo.setSkuId(skuInfo.getSkuId());
        skuVo.setSkuName(skuInfo.getSkuName());
        skuVo.setSkuTitle(skuInfo.getSkuTitle());
        skuVo.setSkuSubtitle(skuInfo.getSkuSubtitle());
        skuVo.setPrice(skuInfo.getPrice());
        // 2.折扣信息
        SmsSkuLadder ladder = smsSkuLadderService.findOneBySkuId(skuId);
        if (null != ladder) {
            skuVo.setFullCount(ladder.getFullCount());
            skuVo.setDiscount(ladder.getDiscount());
            skuVo.setCountStatus(ladder.getAddOther() + "");
        }
        // 3.满减信息
        SmsSkuFullReduction reduction = smsSkuFullReductionRepository.findOneByCondition(
                MapUtil.builder(new HashMap<String, Object>()).put("skuId", skuId).build());
        if (null != reduction) {
            skuVo.setFullPrice(reduction.getFullPrice());
            skuVo.setReducePrice(reduction.getReducePrice());
            skuVo.setPriceStatus(reduction.getAddOther() + "");
        }
        // 4.会员价格
        List<SmsMemberPrice> memberPrices = smsMemberPriceService.findBySkuId(skuId);
        List<MemberPriceVo> memberPrices_ = memberPrices.stream().map(memberPrice -> {
            MemberPriceVo priceVo = new MemberPriceVo();
            priceVo.setId(memberPrice.getMemberLevelId());
            priceVo.setName(memberPrice.getMemberLevelName());
            priceVo.setPrice(memberPrice.getMemberPrice());
            return priceVo;
        }).collect(Collectors.toList());
        skuVo.setMemberPrice(memberPrices_);
        return skuVo;
    }

    public void saveSkuReduction(SkuInfoVo skuVo) throws Exception {
        // 1.基本信息
        PmsSkuInfo skuInfo = pmsSkuInfoService.findById(skuVo.getSkuId());
        skuInfo.setPrice(skuVo.getPrice());
        pmsSkuInfoService.update(skuInfo);
        // 2.折扣信息
        if (skuVo.getFullCount() > 0) {
            SmsSkuLadder ladder = new SmsSkuLadder();
            ladder.setSkuId(skuVo.getSkuId());
            ladder.setFullCount(skuVo.getFullCount());
            ladder.setDiscount(skuVo.getDiscount());
            ladder.setAddOther(skuVo.getCountStatus());
            smsSkuLadderService.insert(ladder);
        }

        // 3.满减信息
        if (skuVo.getFullPrice().compareTo(BigDecimal.ZERO) == 1) {
            SmsSkuFullReduction reduction = new SmsSkuFullReduction();
            reduction.setSkuId(skuVo.getSkuId());
            reduction.setFullPrice(skuVo.getFullPrice());
            reduction.setReducePrice(skuVo.getReducePrice());
            reduction.setAddOther(skuVo.getPriceStatus());
            smsSkuFullReductionRepository.insert(reduction);
        }

        // 4.会员价格
        smsMemberPriceService.deleteBySkuId(skuVo.getSkuId());
        List<MemberPriceVo> members = skuVo.getMemberPrice();
        List<SmsMemberPrice> memberPrices = members.stream().map(vo -> {
            SmsMemberPrice memberPrice = new SmsMemberPrice();
            memberPrice.setSkuId(skuVo.getSkuId());
            memberPrice.setMemberLevelId(vo.getId());
            memberPrice.setMemberLevelName(vo.getName());
            memberPrice.setMemberPrice(vo.getPrice());
            memberPrice.setAddOther("1");
            return memberPrice;
        }).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(memberPrices)) {
            smsMemberPriceService.batchInsert(memberPrices);
        }
    }

    public void deleteSkuReduction(Long skuId) {
        // 1.删除折扣信息
        smsSkuLadderService.deleteBySkuId(skuId);
        // 2.删除满减信息
        smsSkuFullReductionRepository.deleteByProperties("skuId", skuId);
        // 3.删除会员价格
        smsMemberPriceService.deleteBySkuId(skuId);
    }
}
